<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
	<meta charset="UTF-8">
	<meta name="renderer" content="webkit">
	<meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
	<meta name="viewport" content="width=device-width,initial-scale=1,minimum-scale=1,maximum-scale=1,user-scalable=no" />
	<title>网站后台管理模版</title>
	<link rel="stylesheet" type="text/css" th:href="@{/layui/css/layui.css}"/>
	<link rel="stylesheet" type="text/css" th:href="@{/css/admin.css}"/>
</head>
<body>
<form class="layui-form column-content-detail">
	<div class="layui-tab">
		<ul class="layui-tab-title">
			<li class="layui-this">添加用户</li>
		</ul>
		<div class="layui-tab-content">
			<div class="layui-tab-item layui-show">
				<div class="layui-form-item">
					<label class="layui-form-label">账号：</label>
					<div class="layui-input-block">
						<input type="text" id="userName" name="userName" onKeyUp="value=value.replace(/[\W]/g,'')" required lay-verify="required" placeholder="请输入登录账号" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">姓名：</label>
					<div class="layui-input-block">
						<input type="text" id="userNameCn" name="userNameCn" required lay-verify="required" placeholder="请输入姓名" autocomplete="off" class="layui-input">
					</div>
				</div>
				<div class="layui-form-item">
					<label class="layui-form-label">密码：</label>
					<div class="layui-input-block">
						<input type="text" id="password" name="password" onKeyUp="value=value.replace(/[\W]/g,'')" required lay-verify="required" placeholder="请输入登录密码" autocomplete="off" class="layui-input">
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">选择部门</label>
						<div class="layui-input-inline">
							<input type="text" name="deptName" id="deptName" readonly autocomplete="off" lay-verify="required" class="layui-input" placeholder="点击选择部门按钮">
							<input type="hidden" name="deptId" id="deptId" value="">
						</div>
					</div>
					<div class="layui-inline">
						<div class="layui-input-inline">
							<button type="button" class="layui-btn layui-btn-normal" onclick="selectTree()" >选择部门</button>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">选择岗位</label>
						<div class="layui-input-inline">
							<select name="station" lay-verify="required" required autocomplete="off" class="layui-input" id="station">
								<option value="">请选择岗位</option>
							</select>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">选择审批角色（没有可不选）</label>
						<div class="layui-input-inline">
							<select name="examineRole"  autocomplete="off" class="layui-input" id="examineRole">
								<option value="">请选择审批角色</option>
							</select>
						</div>
					</div>
				</div>

				<div class="layui-form-item">
					<div class="layui-inline">
						<label class="layui-form-label">部门负责人</label>
						<div class="layui-input-inline">
							<select name="isLeader" lay-verify="required" id="isLeader">
								<option value="0" selected>否</option>
								<option value="1">是</option>
							</select>
						</div>
					</div>
				</div>
				</div>


			</div>
		</div>
	</div>
	<div class="layui-form-item" style="padding-left: 10px;">
		<div class="layui-input-block">
			<button class="layui-btn layui-btn-normal" lay-submit lay-filter="formDemo">立即提交</button>
			<button type="reset" class="layui-btn layui-btn-primary">重置</button>
		</div>
	</div>
</form>
<script th:src="@{/js/jquery-3.2.1.min.js}" type="text/javascript" charset="utf-8"></script>
<script th:src="@{/layui/layui.js}" type="text/javascript" charset="utf-8"></script>
<script th:src="@{/js/common.js}" type="text/javascript" charset="utf-8"></script>
<script>

    layui.use(['form', 'table', 'laydate', 'layer', 'laypage', 'dialog',  'tool', 'element', 'upload', 'layedit'], function() {
        var form = layui.form,
            layer = layui.layer,
            table = layui.table,
            laypage = layui.laypage,
            laydate = layui.laydate,
            layedit = layui.layedit,
            tool = layui.tool,
            element = layui.element,
            dialog = layui.dialog;

        //获取当前iframe的name值
        var iframeObj = $(window.frameElement).attr('name');

		//监听提交
		form.on('submit(formDemo)', function(data){
			var data = {
				userName:$('#userName').val(),
				userNameCn:$('#userNameCn').val(),
				password:$('#password').val(),
				groupId:$('#deptId').val(),
				groupName:$('#deptName').val(),
				stationName:$("#station").find("option:selected").text(),
				stationId:$("#station").find("option:selected").val(),
				isLeader:$("#isLeader").find("option:selected").val()
			}
			$.ajax({
				url:'/user/add',
				data:data,
				type: 'post',
				dataType:'json',
				success:function (data){
					if(data.code == 200){
						layer.msg('添加成功');
					}else if(data.code == 400){
						layer.msg(data.msg);
					}else{
						layer.msg('添加失败，请刷新页面');
					}
					var index = parent.layer.getFrameIndex(window.name);
					setTimeout(function () {
						//先得到当前iframe层的索引
						parent.layer.close(index);
						//window.parent.location.replace(parent.location.href)
						parent.layui.table.reload('test',{page:{curr:1}});
					},3000)

				}
			});
			return false;
		});



	});

	function renderForm(){//在这里把重新渲染的功能包装成一个方法
		layui.use('form', function(){
			var form = layui.form;//高版本建议把括号去掉，有的低版本，需要加()


			form.render();
		});
	}


	function selectTree() {
		layer.open({
			type: 2,
			title:'部门列表',
			content: '/station/depts',
			area: ['80%', '80%'],
			success: function(layero, index){
			}
		});
	}

	function examineRole(){
		$.ajax({
			url:'/examine/role/allRoles',
			type: 'get',
			dataType:'json',
			success:function (data){
				if(data.code == 200){
					$("#examineRole").empty();
					$("#examineRole").prepend("<option value=\"\">请选择审批角色</option>");//添加第一个option值
					for (var i = 0; i < data.data.length; i++) {
						$("#examineRole").append("<option value='"+data.data[i].id+"'>"+data.data[i].roleName+"</option>");
					}
					//重置表单
					renderForm();
				}else{
					layer.msg('获取角色失败，请刷新页面');
				}

			}
		});
	}

	$(function(){
		// 加载审批角色
		examineRole();

		//输入框的值改变时触发
		$("#deptId").on("change",function(e){
			//获取input输入的值
			console.log(e.delegateTarget.value);
			$.ajax({
				url:'/station/getStations?deptId='+e.delegateTarget.value,
				type: 'get',
				dataType:'json',
				success:function (data){
					if(data.code == 200){
						$("#station").empty();
						$("#station").prepend("<option value=\"\">请选择岗位</option>");//添加第一个option值
						for (var i = 0; i < data.data.length; i++) {
							$("#station").append("<option value='"+data.data[i].id+"'>"+data.data[i].name+"</option>");
						}
						//重置表单
						renderForm();
					}else{
						layer.msg('获取岗位失败，请刷新页面');
					}

				}
			});
		});


	});


</script>
</body>
</html>